<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:project:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:project:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:project:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" type="index" /> -->
      <el-table-column
        prop="code"
        show-overflow-tooltip
        label="合同编号"
        align="center"
        min-width="120"
      ></el-table-column>
      <el-table-column label="项目名称" prop="projectName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">
            {{
            scope.row.projectName
            }}
          </el-link>
        </template>
      </el-table-column>

      <el-table-column
        prop="startTime"
        show-overflow-tooltip
        label="开始时间"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column prop="endTime" show-overflow-tooltip label="结束时间" align="center" width="100"></el-table-column>
      <el-table-column
        prop="leaderName"
        show-overflow-tooltip
        label="项目负责人"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="totalMoney"
        show-overflow-tooltip
        label="合同总金额"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="receiveMoney"
        show-overflow-tooltip
        label="项目总收入"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="payMoney"
        show-overflow-tooltip
        label="项目总支出"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="leaveMoney"
        show-overflow-tooltip
        label="剩余款项"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="progress"
        show-overflow-tooltip
        label="项目进度"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column prop="status" show-overflow-tooltip label="项目状态" align="center" width="100"></el-table-column>
      <!-- <el-table-column prop="projectRate" show-overflow-tooltip label="项目进度" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.projectRate ? scope.row.projectRate : 0 }}%
        </template>
      </el-table-column>-->
      <el-table-column
        v-if="$auth.hasPermi('business:project:status:change')"
        :show-overflow-tooltip="true"
        label="禁用启用"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.projectStatus"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['business:project:info']"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
          v-if="scope.row.projectLinkId == $store.state.user.id"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:project:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:project:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  projectList,
  projectDel,
  effectSupplier
} from "@/api/business/project.js";
import { userSelect, deptTreeSelect } from "@/api/system/user.js";
export default {
  name: "project",
  dicts: ["sys_comp_fee_status", "sys_fee_type"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatusChange(row) {
      let text = row.projectStatus === "1" ? "启用" : "禁用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.projectName + '"吗？')
        .then(function() {
          return effectSupplier(row.projectStatus, row.projectId);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function(e) {
          row.projectStatus = row.projectStatus === "0" ? "1" : "0";
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.projectId || this.ids;
      this.$router.push("./addProject?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      this.$router.push("./addProject?isInfo=true&id=" + row.projectId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.projectId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "projectId",
        "DELETE",
        "projectName"
      );
      this.$modal
        .confirm('是否确认删除项目名称为"' + name + '"的项目？')
        .then(function() {
          return projectDel(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push("./addProject");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      projectList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
